package tools;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import core.DrawContext;
import core.DrawTool;


public class SquareSelectionTool extends DrawTool {
	
	private Rectangle2D.Double shape = new Rectangle2D.Double();
	
	public void processKeyEvent(KeyEvent evt, DrawContext context) {
		
	}

	public void processMouseEvent(MouseEvent evt, DrawContext context) {
		
		switch(evt.getID()){
		case MouseEvent.MOUSE_PRESSED:
			mousePressed(evt, context);
			break;
		case MouseEvent.MOUSE_RELEASED:
			mouseReleased(evt, context);
			break;
		case MouseEvent.MOUSE_DRAGGED:
			mouseDrag(evt, context);
			break;
		}
		
	}
	
	private void mousePressed(MouseEvent evt, DrawContext context){
		
		if(context.hasSelection()){
			clearSelection(context);
		}
				
		shape.x = evt.getX();
		
		shape.y = evt.getY();
		
		context.setSelectionShape(shape);
		
		context.setChanged(true);
	}
	
	private void mouseReleased(MouseEvent evt, DrawContext context){
	}
	
	private void mouseDrag(MouseEvent evt, DrawContext context){
		
		shape.width = evt.getX() - shape.x;
		
		shape.height = evt.getY() - shape.y;
		
		context.setChanged(true);
		context.setSelection(true);
		
	}
	
	private void clearSelection(DrawContext context){
	
		shape.width = 0;
		shape.height = 0;
		
		context.setSelection(false);
		context.setSelectionShape(null);
		
	}

	@Override
	public void dispose(DrawContext ctx) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(DrawContext ctx) {
		// TODO Auto-generated method stub
		
	}
	
}
